/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ActionForm;

import java.util.regex.*;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlElementDecl.GLOBAL;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author cpn02
 */
public class RegisterForm extends ActionForm {
    private String userName;
    private String passw;
    private String email;
    private String phone;
    private String address;
    private String repassw;
    private int gender;

    public String getRepassw() {
        return repassw;
    }

    public void setRepassw(String repassw) {
        this.repassw = repassw;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassw() {
        return passw;
    }

    public void setPassw(String passw) {
        this.passw = passw;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public int getGender()
    {
        return gender;
    }
    public void setGender(int gender)
    {
        this.gender = gender;
    }
    
    public boolean isnull(String st)
    {
        if(st==null||st.trim()=="")
            return true;
        else
            return false;
    }
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if(isnull(userName)||isnull(passw)||isnull(repassw)||isnull(email)||isnull(phone)||isnull(address))
        {
            errors.add("GLOBAL-ERROR",new ActionMessage("errors.register.null"));
        }
        else
        {
        if(passw.length()<6)
        {
            errors.add("passw",new ActionMessage("errors.register.passwlenght"));
        }
        else
       if(!passw.equals(repassw))
       {
           errors.add("repassw", new ActionMessage("errors.register.repassw"));
       }
       String reg = "[a-zA-Z]\\w+\\@[a-zA-Z]\\w+(.[a-zA-Z]\\w+)+";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(email);
        if(!m.matches())
            errors.add("email",new ActionMessage("errors.register.email"));
        reg = "[0-9]+";
        p = Pattern.compile(reg);
        m = p.matcher(phone);
        if(!m.matches())
            errors.add("phone", new ActionMessage("errors.register.phone"));
        }
        return errors;
    }
    
}
